from gen import generate_test_cases, generate_duplicate_string
from alg import Solution

def test_remove_duplicate_letters():
    """
    测试去除重复字母算法
    """
    solution = Solution()
    
    print("=" * 50)
    print("去除重复字母算法测试")
    print("=" * 50)
    
    # 生成测试用例
    test_cases = generate_test_cases(8)
    
    print(f"共生成 {len(test_cases)} 个测试用例：\n")
    
    for i, test_case in enumerate(test_cases, 1):
        result = solution.removeDuplicateLetters(test_case)
        
        print(f"测试用例 {i}:")
        print(f"  输入: {test_case}")
        print(f"  输出: {result}")
        print(f"  长度: {len(test_case)} -> {len(result)}")
        
        # 验证结果的正确性
        unique_chars = set(test_case)
        result_chars = set(result)
        
        if unique_chars == result_chars:
            print(f"  ✓ 包含所有唯一字符")
        else:
            print(f"  ✗ 字符不完整")
        
        # 检查字典序
        if list(result) == sorted(result):
            print(f"  ✓ 字典序正确")
        else:
            print(f"  - 字典序: {result}")
        
        print()

def performance_test():
    """
    性能测试
    """
    print("=" * 50)
    print("性能测试")
    print("=" * 50)
    
    import time
    solution = Solution()
    
    # 测试不同长度的字符串
    lengths = [100, 500, 1000, 5000]
    
    for length in lengths:
        test_string = generate_duplicate_string(length)
        
        start_time = time.time()
        result = solution.removeDuplicateLetters(test_string)
        end_time = time.time()
        
        print(f"长度 {length}: {end_time - start_time:.6f} 秒")
        print(f"  原始长度: {len(test_string)}, 结果长度: {len(result)}")

if __name__ == "__main__":
    test_remove_duplicate_letters()
    performance_test()
